package cn.ecnu.controller.forum;

import cn.ecnu.common.utils.R;
import cn.ecnu.pojo.forum.Forum;
import cn.ecnu.service.forum.ForumService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/circle/forum")
public class ForumController {

    @Resource
    private ForumService forumService;

    // 获取论坛的列表
    @GetMapping("/getForumList")
    public R getForumList() {
        List<Forum> forumList = forumService.getForumList();
        return R.ok().put("data", forumList);
    }

    // 根据论坛id获取论坛
    @GetMapping("/getForumById/{forumId}")
    public R getForumById(@PathVariable("forumId") Integer forumId) {
        Forum forum = forumService.getForumById(forumId);
        return R.ok().put("data", forum);
    }

    // 根据论坛名称获取论坛
    @GetMapping("/getForumByName/{name}")
    public R getForumByName(@PathVariable("name") String name) {
        Forum forum = forumService.getForumByName(name);
        return R.ok().put("data", forum);
    }

    @PostMapping("/insertForum")
    public R insertForum(@RequestBody Forum forum) {
        forum.setPostCount(0); forum.setHot(0); // 校园圈初始化
        forumService.insertForum(forum);
        return R.ok().put("data", forum);
    }

    @PostMapping("/updateForum")
    public R updateForum(@RequestBody Forum forum) {
        forumService.updateForum(forum);
        return R.ok().put("data", forum);
    }
}
